<?php
/**
 * Author: lf
 * Blog: https://blog.feehi.com
 * Email: job@feehi.com
 * Created at: 2017-08-30 18:10
 */

namespace api\controllers;

use abei2017\wx\Application;
use api\models\User;

use yii\base\Model;
use yii\web\Response;
use Yii;
use yii\web\ServerErrorHttpException;

class UserController extends BaseController
{
    public $modelClass = "api\models\User";

    public $enableCsrfValidation = false;

    public function actionLogin0()
    {
        if (\Yii::$app->getRequest()->isPost) {
//            $user               = new User();
            $user               = User::findOne(2);
            $user->access_token = Yii::$app->security->generateRandomString();
//            $user->wx_openid    = "111222";
//            $user->username     = "mars-test";
            $user->save(false);

            return [
                'access-token' => $user->access_token,
                'user'         => $user,
            ];
        }
        return ['msg' => ''];
    }


    public function actionLogin()
    {
        if (\Yii::$app->getRequest()->isPost) {

            $code        = \Yii::$app->getRequest()->post('code');
            $wx_user     = (new Application())->driver("mini.user");
            $result      = $wx_user->codeToSession($code);
//            $openid      = $result['openid'];
            $openid      = $result['openid'];
            $session_key = $result['session_key'];
            //TODO:没有oppenid则为空
            if (User::findOne(['wx_openid' => $openid])) { //用户存在
                $date               = \Yii::$app->getRequest()->post('user');
                $user               = User::findOne(['wx_openid' => $openid]);
                $user->wx_avatar    = $date['avatar'];
                $user->wx_name      = $date['name'];
                $user->access_token = Yii::$app->security->generateRandomString();
                $user->save(false);
            } else {
                $date                 = Yii::$app->getRequest()->post('user');
                $user                 = new User();
                $user->wx_avatar      = $date['avatar'];
                $user->wx_name        = $date['name'];
                $user->username       = $user->wx_name; //初始化时用户名和微信名相同
                if (empty($user->username)) {
                    $user->username = date("m-d-H-i-s");
                }
                $user->wx_openid      = $openid;
                $user->wx_session_key = $session_key;
                $user->access_token   = Yii::$app->security->generateRandomString();
                $user->save(false);
            }

            if ($user->status == User::STATUS_DELETED) {
                return [
                    'access_token' => $user->access_token,
                    'user'         => $user,
                    'forbidden'    => '1',
                ];
            }

            return [
                'access_token' => $user->access_token,
                'user'         => $user,
                'forbidden'    => '0',
            ];
        }

        return ['msg' => ''];
    }

    public function actionUpdateSelf()
    {
        $model = User::findOne(Yii::$app->getUser()->id);
        $this->checkAccess($this->id, $model);
        $model->scenario = Model::SCENARIO_DEFAULT;
        $model->load(Yii::$app->getRequest()->getBodyParams(), '');
        if ($model->save() === false && ! $model->hasErrors()) {
            throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
        }
        return $model;
    }

    public function actionViewSelf(){
        $model = User::findOne(Yii::$app->getUser()->id);
        return [
            $model,
            'rbac'=>Yii::$app->authManager->getPermissions()
        ];
    }



}
